<?php
class Dmf_Service_Twitter
{
    private $_user_show = 'https://api.twitter.com/1/users/show.json?include_entities=true&';
    
    private $_http;
    
    public function __construct()
    {
        $this->_http = new Dmf_Http();
    }
    
    public function getUser( $user_id_or_name )
    {
        if ( is_numeric( $user_id_or_name ) )
        {
            $params = array(
                'user_id' => $user_id_or_name
            );
        }
        else
        {
            $params = array(
                'screen_name' => $user_id_or_name
            );
        }
        
        $return = $this->parseTwitterInfo( $this->_user_show, $params );
        
        return new Dmf_Service_Twitter_User( $return );
    }
    
    public function parseTwitterInfo( $url, $params )
    {
        $query = '';
        
        foreach( $params as $key => $value )
        {
            $query .= $key . '=' . $value . '&';
        }
        
        $get = $this->_http->get( $url . $query );
        
        if ( ! $get->ok() )
        {
            throw new Exception( 'Something went wrong with the TWITTER-API could not create request.<br /><small>' . $get->getError() . '</small>' );
        }
        
        $info = (array)$get->getJson();
        
        if ( isset( $info['error'] ) )
        {
            throw new Dmf_Exception_TwitterAPI( $info['error'] . ' on request ' . $info['request'] );
        }
        
        return $info;
    }
}